package com.lmiot.xyclick.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CarNumUtil {
    private static final int CPU_COUNT;
    private static final String TAG = "CarNumUtil";
    private static final int corePoolSize;
    private static final int maximumPoolSize;
    private static final CarNumUtil ourInstance = new CarNumUtil();
    private ExecutorService fixedThreadPool = Executors.newFixedThreadPool(CPU_COUNT + 1);

    static {
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        CPU_COUNT = availableProcessors;
        corePoolSize = Math.max(2, Math.min(availableProcessors - 1, 4));
        maximumPoolSize = (availableProcessors * 2) + 1;
    }

    public static CarNumUtil getInstance() {
        return ourInstance;
    }

    private CarNumUtil() {
    }

    public static String sendPost(String str, String str2) {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.setRequestProperty("Accept", "*/*");
            httpURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
            httpURLConnection.setRequestProperty("Connection", "keep-alive");
            StringBuilder sb = new StringBuilder();
            sb.append("BAIDUID=");
            sb.append(strToMD5(System.currentTimeMillis() + ""));
            sb.append(":FG=1");
            httpURLConnection.setRequestProperty("Cookie", sb.toString());
            httpURLConnection.setRequestProperty("Origin", "https://cloud.baidu.com");
            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            httpURLConnection.setRequestProperty("Content-Length", str2.getBytes().length + "");
            httpURLConnection.setRequestProperty("Referer", "https://cloud.baidu.com/product/ocr/general");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.getOutputStream().write(str2.getBytes());
            httpURLConnection.getOutputStream().flush();
            if (httpURLConnection.getResponseCode() == 200) {
                return readIO(httpURLConnection.getInputStream());
            }
        } catch (Exception e) {
            PrintStream printStream = System.out;
            printStream.println("发送 POST 请求出现异常！" + e.toString());
            e.printStackTrace();
        }
        return "";
    }

    public static byte[] readFile(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bArr = new byte[1024];
            while (true) {
                int read = fileInputStream.read(bArr);
                if (read == -1) {
                    return byteArrayOutputStream.toByteArray();
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return new byte[0];
        } catch (IOException e2) {
            e2.printStackTrace();
            return new byte[0];
        }
    }

    public static String readIO(InputStream inputStream) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bArr = new byte[1024];
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    return new String(byteArrayOutputStream.toByteArray(), "UTF-8");
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e2) {
            e2.printStackTrace();
            return "";
        }
    }

    public void OCR(String str) {
        LogUtil.d(TAG, "开始识别：" + str);
        final String replace = "image={base64}&image_url=&type=license_plate&detect_direction=false".replace("{base64}", URLEncoder.encode("data:image/png;base64," + ImgUtil.imageToBase64(str)));
        this.fixedThreadPool.execute(new Runnable() {
            /* class com.lmiot.xyclick.Util.CarNumUtil.AnonymousClass1 */

            public void run() {
                try {
                    String sendPost = CarNumUtil.sendPost("http://cloud.baidu.com/aidemo", replace);
                    LogUtil.d(CarNumUtil.TAG, "识别成功：" + sendPost);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static String strToMD5(String str) {
        String str2 = new String();
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(str.getBytes());
            byte[] digest = instance.digest();
            StringBuffer stringBuffer = new StringBuffer("");
            for (byte b : digest) {
                int i = b;
                if (b < 0) {
                    i = b + 256;
                }
                if (i < 16) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(Integer.toHexString(i == 1 ? 1 : 0));
            }
            return stringBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return str2;
        }
    }
}
